package com.tarena.lbs.test.es;

import com.alibaba.fastjson2.JSON;
import com.tarena.lbs.test.es.pojo.Student;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * es文档操作
 */
public class ESDocTestRun {
    private RestHighLevelClient client;
    @Before
    public void initClient(){
        //构造一个可以连接本地localhost:9200的client对象
        HttpHost host=new HttpHost("localhost",9200,"http");
        RestClientBuilder builder = RestClient.builder(host);
        client=new RestHighLevelClient(builder);
    }

    //创建文档,类似数据库插入一条数据
    @Test
    public void createDoc() throws IOException {
        //1 创建文档对应的请求
        IndexRequest request=new IndexRequest();
        //2 根据需求填写请求参数
        //2.1 指定索引名称
        request.index("test03");
        //2.2 指定文档数据
        request.source(
                "{\"name\":\"王翠花\",\"gender\":\"女\",\"birthday\":\"2000-06-19\",\"location\":\"39.9042,116.4074\",\"student_no\":\"20210001\"}",
                XContentType.JSON);
        //2.3 文档id
        request.id("20210001");
        //3 执行请求
        client.index(request, RequestOptions.DEFAULT);
    }
    //利用文档id 获取文档
    @Test
    public void getDoc() throws IOException {
        //1.构建一个查询某个文档的请求
        GetRequest request=new GetRequest();
        //1.1.指定索引
        request.index("test03");
        //1.2.指定文档id
        request.id("20210001");
        //2. 读取索引文档的数据 不仅仅包含写入的json字符串 对象
        GetResponse docResponse = client.get(request, RequestOptions.DEFAULT);
        //2.1 从响应中获取文档数据一些元属性
        String index = docResponse.getIndex();
        System.out.println("es的索引:"+index);
        //2.2 获取不同格式的文档数据内容 string map
        Map<String, Object> sourceAsMap = docResponse.getSourceAsMap();
        sourceAsMap.forEach((k,v)->{
            System.out.println("k="+k+";v="+v);
        });
        String sourceAsString = docResponse.getSourceAsString();
        System.out.println(sourceAsString);

        //json字符串转java对象
        Student student = JSON.parseObject(sourceAsString, Student.class);
        System.out.println("json字符串转java对象:"+student);
        //map对象转java对象,先把map转json,再把json转java对象
        Student student1 = JSON.parseObject(JSON.toJSONString(sourceAsMap), Student.class);
        System.out.println("map转java对象:"+student1);
    }
    //更新文档 部分更新
    @Test
    public void updateDoc() throws IOException {
        //1.根据文档id 更新文档部分数据
        UpdateRequest request=new UpdateRequest("test03","20210001");
        //2.携带更新的内容
        Map<String,Object> updateFields=new HashMap<>();
        updateFields.put("gender","男");
        updateFields.put("name","刘翠花");
        request.doc(updateFields);
        //3.执行请求
        client.update(request,RequestOptions.DEFAULT);
    }
    //删除文档 利用id 删除某个索引中的文档请求
    @Test
    public void deleteDoc() throws IOException {
        //1.根据文档id删除
        DeleteRequest deleteRequest=new DeleteRequest("lbs_store","null");
        //.执行请求
        DeleteResponse delete = client.delete(deleteRequest,RequestOptions.DEFAULT);
        System.out.println("删除成功");
    }
}
